//
//  YSGNurseAppointmentOrderViewModel.swift
//  EyeVisionForPatients
//
//  Created by liuyutian on 2022/4/2.
//  Copyright © 2022 QDSG. All rights reserved.
//

import HandyJSON
import UIKit


//订单状态  /**      *待支付      */     UNPAID(0, "待支付"),     /**      *待接单      */     WAIT_ACCEPT(1, "待接单"),     /**      *进行中      */     UNDERWAY(2, "进行中"),     /**      * 已结束      */     FINISH(3, "已结束"),     /**      * 已评价      */     EVALUATE(4, "已评价"),     /**      *退款中      */     IN_REFUND(5, "退款中"),     /**      *已退款      */     REFUND(6, "已退款"),     /**      *已取消      */     CANCEL(10, "已取消");

enum nurseStatusEnum: Int, HandyJSONEnum {
    case unpaid = 0,
     waitAccpet = 1,
     underway = 2,
     finish = 3,
     evalute = 4,
     inRefund = 5,
     refund = 6,
     cancel = 10
    
    var title :String{
        switch self {
        case .unpaid:
            return "未支付"
        case .waitAccpet:
            return "待接单"
        case .underway:
            return "进行中"
        case .finish:
            return "已结束"
        case .evalute:
            return "已评价"
        case .inRefund:
            return "退款中"
        case .refund:
            return "已退款"
        case .cancel:
            return "已取消"
        }
    }
    var titleColor:UIColor{
        switch self {
        case .unpaid,.waitAccpet,.finish,.inRefund:
            return UIColor.warning
        case .cancel:
            return UIColor.textLight
        default:
            return UIColor.mainColor
        }
    }
    
}

enum orderStatusEnum: Int, HandyJSONEnum {
    case unpaid = 10,cancel = 20,paying = 30,payed = 35,unEvaluated = 40,refund = 50,refunded = 60,evaluated = 70,breakApp = 80, payFail = 90,
    unpay = 401,unDeliver = 402,deliver = 403,receiveing = 404,unReceive = 405, received = 406,
    receiveCanel = 407,receiveRefund = 408,receiveRefunded = 409,payedQuery = 509,error = 900
    var title :String{
        switch self {
        case .unpaid:
            return "待支付"
        case .cancel:
            return "已取消"
        case .paying:
            return "支付中"
        case .payed:
            return "已付款"
        case .unEvaluated:
            return "待评价"
        case .refund:
            return "退款中"
        case .refunded:
            return "已退款"
        case .evaluated:
            return "已评价"
        case .breakApp:
            return "爽约"
        case .payFail:
            return "医保支付失败"
        case .unpay:
            return "待支付"
        case .unDeliver:
            return "未发货"
        case .deliver:
            return "已发货"
        case .receiveing:
            return "已收货"
        case .unReceive:
            return "待领取"
        case .received:
            return "已领取"
        case .receiveCanel:
            return "已取消"
        case .receiveRefund:
            return "退款中"
        case .receiveRefunded:
            return "已退款"
        case .payedQuery:
            return "已支付,未回调"
        case .error:
            return "订单异常"
        }
    }
    

    
}

enum appointmentButtonStatusEnum: Int, HandyJSONEnum {
    case all, cancel,completed,evaluated
}


enum appointmentStatusEnum: Int, HandyJSONEnum {
    case unpaid = 0,notApproved_unassigned = 1,notApproved_missedOrders = 2, reserved = 3, cancel = 4,completed = 5,evaluated = 6
    var title:String{
        switch self {
        case .unpaid:
            return "未支付"
        case .notApproved_unassigned,.notApproved_missedOrders:
            return "待审核"
        case .reserved:
            return "已预约"
        case .cancel:
            return "已取消"
        case .evaluated:
            return "已评价"
        case .completed:
            return "已完成"
        }
    }
    var titleColor:UIColor{
        switch self {
        case .unpaid,.notApproved_unassigned,.notApproved_missedOrders:
            return UIColor.warning
        case .cancel:
            return UIColor.textLight
        default:
            return UIColor.mainColor
        }
    }
    
    var buttonStatus : appointmentButtonStatusEnum{
        switch self {
        case .unpaid:
            return .all
        case .notApproved_unassigned:
            return .cancel
        case .notApproved_missedOrders:
            return .cancel
        case .reserved:
            return .cancel
        case .cancel:
            return .evaluated
        case .completed:
            return .completed
        case .evaluated:
            return .evaluated
        }
    }
    
    var rowheight: Float{
        switch self {
        case .unpaid:
            return 274.0
        case .notApproved_unassigned,.notApproved_missedOrders,.reserved,.cancel,.completed,.evaluated:
            return 227.0
        }
    }
}

struct YSGNurseFindPatientOrderModel:HandyJSON{
    var diagnoseId: String? = ""  // 咨询id
    var finishTime: String?  = ""  // 结束时间
    var nurseAvatar: String? = ""   // 护士头像
    var nurseName: String? = ""  // 护士名称
    var orderTime: String? = ""  // 预约时间
    var patientId: String? = ""  // 病人id
    var patientName: String? = ""  // 病人id
    var status : nurseStatusEnum? //状态
    var orderId : String? = "" //订单id
}

struct YSGNurseAppointmentOrderModel:HandyJSON{
    var orderId: String?

    var birth: String?

    var patientPhone: String?

    var payWay: Int = 0

    var isEyeRed: Int = 0

    var isRemove: Int = 0

    var orderStatus: orderStatusEnum?

    var cancelReason : String = ""
    
    var isSetOut: Int = 0

    var patientName: String = ""

    var appointmentTime: String?

    var sex: String?

    var appointmentStatus: appointmentStatusEnum?

    var appointmentId: String?

    var orderAmount: String?

    var sendPayTime: String?

    var nurseCompleteTime: String?

    var isUnobstructed: Int = 0

    var commentContent: String?

    var isSecretion: Int = 0

    var commentScore: String?

    var nurseStatus: Int = 0

    var suturePosition: String?

    var isTrichiasis: Int = 0

    var createTime: String = ""

    var adress: String = ""

    var age: Int = 0

    var nursePhone: String?

    var isSuture: Int = 0

    var orderNumber: String?

    var nurseReceivingTime: String?

    var projectTitle: String?

    var nurseName: String?
    
    var leftMin : String?
    
    var consumeMaterials: [YSGNurseAppointmentInfoConsumablessModel] = []
    
    static func getNurseName(_ nuserName: String)-> NSAttributedString
    {
        let nurseNameString = NSMutableAttributedString(
            string: nuserName + " 护士",
            attributes: [NSAttributedString.Key.font:
                UIFont(name: "PingFang SC", size: 12.0)!])
        nurseNameString.addAttribute(NSAttributedString.Key.foregroundColor,value:UIColor.textDark, range: NSRange(location: 0,length: nuserName.count))
        nurseNameString.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "PingFangSC-Semibold", size: 14.0)!, range: NSRange(location: 0,length: nuserName.count ))
        nurseNameString.addAttribute(NSAttributedString.Key.foregroundColor,value:UIColor.textGray, range: NSRange(location: nuserName.count ,length: 2))

         return nurseNameString
    }

    
}

class YSGNurseAppointmentOrderViewModel: NSObject {
    
    func rowHeight(_ status: appointmentStatusEnum?) -> Float{
        switch status{
        case .unpaid:
            return 274.00
        case .cancel:
            return 204.00
        default:
            return 227.00
        }
    }
    
    func getfindPatientOrders(page : Int,finished: @escaping (_ models:[YSGNurseAppointmentOrderModel]?, _ errorMsg: String?, _ code: HttpCode,_ noMoreData:Bool) -> Void) {
        HttpTool.shared.request(target: ApiManager.appointment_get_list(page: page), success: { (json, code) in
            
            switch code {
            case .failure:
                finished([] , json["message"].description, .failure,json["data"]["end"].boolValue)
            case .success:
                if let models = JSONDeserializer<YSGNurseAppointmentOrderModel>.deserializeModelArrayFrom(json: json["data"]["records"].description) as? [YSGNurseAppointmentOrderModel]{
                    finished(models, nil, .success ,json["data"]["end"].boolValue)
                } else {
                    finished([], json["message"].description, .failure,json["data"]["end"].boolValue)
                }
            }
        }) { errorMsg in
            finished([], errorMsg, .failure,false)
        }
    }
    func getNurseList(page : Int,finished: @escaping (_ models:[YSGNurseFindPatientOrderModel]?, _ errorMsg: String?, _ code: HttpCode,_ dataCount:Int) -> Void) {
        HttpTool.shared.request(target: ApiManager.appointment_findPatientOrders(page: page), success: { (json, code) in
            
            switch code {
            case .failure:
                finished([] , json["message"].description, .failure,json["total"].intValue)
            case .success:
                if let models = JSONDeserializer<YSGNurseFindPatientOrderModel>.deserializeModelArrayFrom(json: json["data"]["records"].description) as? [YSGNurseFindPatientOrderModel]{
                    finished(models, nil, .success ,json["total"].intValue)
                } else {
                    finished([], json["message"].description, .failure,json["total"].intValue)
                }
            }
        }) { errorMsg in
            finished([], errorMsg, .failure,0)
        }
    }
    
    func getOrderList(page : Int,finished: @escaping (_ models:[YSGNurseAppointmentOrderModel]?, _ errorMsg: String?, _ code: HttpCode,_ noMoreData:Bool) -> Void) {
        HttpTool.shared.request(target: ApiManager.appointment_get_list(page: page), success: { (json, code) in
            
            switch code {
            case .failure:
                finished([] , json["message"].description, .failure,json["data"]["end"].boolValue)
            case .success:
                if let models = JSONDeserializer<YSGNurseAppointmentOrderModel>.deserializeModelArrayFrom(json: json["data"]["list"].description) as? [YSGNurseAppointmentOrderModel]{
                    finished(models, nil, .success ,json["data"]["end"].boolValue)
                } else {
                    finished([], json["message"].description, .failure,json["data"]["end"].boolValue)
                }
            }
        }) { errorMsg in
            finished([], errorMsg, .failure,false)
        }
    }
    
    func orderCancel(_ orderId : String,finished: @escaping (_ errorMsg: String?, _ code: HttpCode) -> Void) {
        HttpTool.shared.request(target: ApiManager.appointment_cancel(paras: ["orderId":orderId,"type":"1"]), success: { (json, code) in
            
            switch code {
            case .failure:
                finished(json["message"].description, .failure)
            case .success:
                finished(json["message"].description, .success)
            }
        }) { errorMsg in
            finished(errorMsg, .failure)
        }
    }
    func patientCancelOrder(_ diagnoseId : String,finished: @escaping (_ errorMsg: String?, _ code: HttpCode) -> Void) {
        HttpTool.shared.request(target: ApiManager.patient_cancel_order(paras: ["diagnoseId":diagnoseId]), success: { (json, code) in
            
            switch code {
            case .failure:
                finished(json["message"].description, .failure)
            case .success:
                finished(json["message"].description, .success)
            }
        }) { errorMsg in
            finished(errorMsg, .failure)
        }
    }
    
    
    func orderComment(_ appointmentId : String,commentContent : String,commentScore : String ,finished: @escaping (_ errorMsg: String?, _ code: HttpCode) -> Void) {
        HttpTool.shared.request(target: ApiManager.appointment_comment(paras: ["appointmentId":appointmentId,"commentContent":commentContent,"commentScore":commentScore]), success: { (json, code) in
            
            switch code {
            case .failure:
                finished(json["message"].description, .failure)
            case .success:
                finished(json["message"].description, .success)
            }
        }) { errorMsg in
            finished(errorMsg, .failure)
        }
    }
    
    
}
